package com.sc.nft.consume;

import cn.hutool.core.util.ObjectUtil;
import com.sc.nft.dao.UserInfoDao;
import com.sc.nft.entity.IdcardInfo;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.enums.*;
import com.sc.nft.enums.task.TaskOrderTypeEnum;
import com.sc.nft.enums.task.TaskTypeEnum;
import com.sc.nft.service.TaskService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;

@RocketMQMessageListener(topic = MessageTopic.USER_AUTH_TOPIC+"${spring.profiles.active}",consumerGroup =  MessageTopic.USER_AUTH_TOPIC+"${spring.profiles.active}")
@Component
@Slf4j
@RequiredArgsConstructor
public class UserAuthConsumer implements RocketMQListener<IdcardInfo> {

    private final TaskService taskService;
    private final UserInfoDao userInfoDao;

    @Override
    public void onMessage(IdcardInfo idcardInfo) {
        UserInfo userInfo = userInfoDao.getById(idcardInfo.getUserId());
        taskService.finishTask(userInfo, TaskOrderTypeEnum.REAL_NAME,TaskTypeEnum.NEW_AUTH,idcardInfo.getId(),null, BigDecimal.ZERO);
        UserInfo inviteUser = userInfoDao.getById(userInfo.getRecommendUserId());
        if(ObjectUtil.isNull(inviteUser)){
            return;
        }
        taskService.finishTask(inviteUser, TaskOrderTypeEnum.REAL_NAME,TaskTypeEnum.OPENING,idcardInfo.getId(),null, BigDecimal.ZERO);
    }
}
